package datastructure.work;

import java.io.FileWriter;
import java.io.IOException;
import java.util.Comparator;
import java.util.TreeMap;

/**
 * @Author Gan
 * @Date 2021/5/11 20:24
 * @Description: 已经存在5个学生信息(Student) (姓名，语文成绩，数学成绩，英语成绩)，
 * 并按照指定的格式，总分从高到低，输出到文件(输出一个文件出来)。
 */
public class DemoWork2 {
    public static void main(String[] args) throws IOException {
        Student stu1 = new Student("zs", 98.0, 79.8, 67);
        Student stu2 = new Student("ls", 87.5, 56.6, 56.9);
        Student stu3 = new Student("wu", 45.8, 98.6, 60.8);
        Student stu4 = new Student("zl", 96.8, 86.9, 46);
        Student stu5 = new Student("we", 74.7, 86, 67);
        FileWriter fileWriter = new FileWriter("a.txt");
        TreeMap<Student, Double> treeMap = new TreeMap<>(new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                double n = o2.totalScore - o1.totalScore;
                return (int)n;
            }
        });
        treeMap.put(stu1,stu1.totalScore);
        treeMap.put(stu2,stu2.totalScore);
        treeMap.put(stu3,stu3.totalScore);
        treeMap.put(stu4,stu4.totalScore);
        treeMap.put(stu5,stu5.totalScore);
        fileWriter.write(String.valueOf(treeMap));
        fileWriter.close();
    }
}

class Student {
    private String name;
    private double mathScore;
    private double englishScore;
    private double chineseScore;
    double totalScore;

    public Student(String name, double mathScore, double englishScore, double chineseScore) {
        this.name = name;
        this.mathScore = mathScore;
        this.englishScore = englishScore;
        this.chineseScore = chineseScore;
        totalScore = mathScore + englishScore + chineseScore;
    }

    @Override
    public String toString() {
        return "{" +
                "name='" + name + '\'' +
                ", mathScore=" + mathScore +
                ", englishScore=" + englishScore +
                ", chineseScore=" + chineseScore +
                ", totalScore=" + totalScore +
                '}';
    }
}